package com.example.demo.utli;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis 工具类
 *
 * @author SongRenShuo
 * @date 2024/01/09
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 写入对象
     *
     * @param key        键
     * @param value      值
     * @param expireTime 过期时间
     * @return boolean
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public boolean setObject(final String key, Object value, Integer expireTime) {
        try {

            redisTemplate.opsForValue().set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 获取对象
     *
     * @param key 键
     * @return {@link Object }
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public Object getObject(final String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }


    /**
     * 写入集合
     *
     * @param key        键
     * @param value      值
     * @param expireTime 过期时间
     * @return boolean
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public boolean setList(final String key, Object value, Integer expireTime) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取集合
     *
     * @param key 键
     * @return {@link List }<{@link Object }>
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public List<Object> getList(final String key) {
        try {
            return redisTemplate.opsForList().range(key, 0, -1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean setHashAsKV(String key, Object hk, Object hv, Integer expireTime) {
        try {
            redisTemplate.opsForHash().put(key, hk, hv);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean setHashAsMap(String key, Map map, Integer expireTime) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取
     *
     * @param key 键
     * @return {@link Map }
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public Map getMap(String key) {
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        return map;
    }

    /**
     * 获取哈希对象
     *
     * @param k1 K1
     * @param k2 K2
     * @return {@link Object }
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public Object getHashObject(String k1, String k2) {
        return redisTemplate.opsForHash().get(k1, k2);
    }

    /**
     * 判断是否存在key
     *
     * @param key key
     * @return boolean
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public boolean hasKey(final String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除key
     *
     * @param key key
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public void del(final String key) {
        if (hasKey(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param keys keys
     * @return boolean
     * @date: 2024/01/09
     * @author SongRenShuo
     */
    public boolean delKeys(String... keys) {
        for (String key : keys) {
            if (hasKey(key)) {
                Boolean flag = redisTemplate.delete(key);
                if (flag == null) {
                    continue;
                }
                if (!flag) {
                    return false;
                }
            }
        }
        return true;
    }
}
